package ioperations;

import book.BookList;
import book.Book;
import java.util.Scanner;

public class DelOperation implements IOPeration{
    public void work(BookList bookList){
        System.out.println("删除图书....");
        System.out.println("请输入你要删除的书名");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        //找到这本书
        int pos=0;
        int i =0;
        for (i = 0; i < bookList.GetUsedSize() ; i++) {
            Book book = bookList.books[i];
            if(book.getName().equals(name)){
                pos = i;
                break;
            }
        }
        if(i == bookList.GetUsedSize()) {
            System.out.println("没有你要删除的书！");
            return ;
        }

        //删除这本书
        for (int j = pos; j < bookList.GetUsedSize()-1; j++) {
            Book book = bookList.books[j+1];
            bookList.SetBook(j,book);
        }
        bookList.SetBook(bookList.GetUsedSize()-1,null );
        int a=bookList.GetUsedSize();
        bookList.SetUsedSize(a-1);
        System.out.println("删除成功！！！！");
    }
}
